package com.hp.attendance.serlvet.history.InService;

import com.hp.attendance.entity.Employee;
import com.hp.attendance.service.EmployeeBysService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/updEmployee")
public class EmployeeUpdServlet extends HttpServlet {
    private EmployeeBysService employeeBysService = new EmployeeBysService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            String gender = req.getParameter("gender");
            String birthday = req.getParameter("birthday");
            String employee_number = req.getParameter("employee_number");
            String telephone = req.getParameter("telephone");
            String email = req.getParameter("email");
            String address = req.getParameter("address");
            String education = req.getParameter("education");
            String department_number = req.getParameter("department_number");
            String position_number = req.getParameter("position_number");
            String status = req.getParameter("status");
            String notes = req.getParameter("notes");
            String in_time = req.getParameter("in_time");

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date birth = format.parse(birthday.trim());
            Date intimes = format.parse(in_time.trim());

            if (status.equals("离职")||status.equals("退休")){
                String date = req.getParameter("date");
                Date date1 = format.parse(date.trim());
                employeeBysService.UpdateOut_timeByNum(date1, Integer.parseInt(employee_number));
            }
            Employee employee = new Employee(null, Integer.parseInt(employee_number), name, gender, birth, telephone, email, address, null, education,
                    Integer.parseInt(department_number), Integer.parseInt(position_number), intimes, password, notes);

            employeeBysService.UpdateEmployeeByNum(employee);
            employeeBysService.UpdateHistoryByNum(status, Integer.parseInt(employee_number));
            //跳转
            req.getRequestDispatcher("EmployeeList").forward(req, resp);

        } catch (ParseException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
